import sys
import time
import os

__all__ = ['rm_file', 'exit_for_loader', 'log']
ERR_LOG = './err.jnk'



def rm_file(filename):
    if os.path.exists(filename):
        os.remove(filename)

def exit_for_loader(return_code, err_message):
    with open(ERR_LOG, 'w') as f:
        if return_code != 0:
            f.write(str(return_code) + "\t0\n" + '1\t9001\tE\t0\t0\t' + err_message + "\n")
            print(err_message, file=sys.stderr)
            sys.exit(return_code)
        else:
            f.write("0\t0\t\n")
            sys.exit(0)

def log(msg, mode='a'):
    LOG_FIL = sys.argv[0] + '.log'
    LOG_FIL = "debug" + '.log'
    with open(LOG_FIL, mode) as LOG:
        TIME = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
        LOG.write(TIME + '\n' + str(msg) + '\n')
